package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle>
        implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    /**
     * 根据参数加载文章列表
     *
     * @param type 1为加载更多  2为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {

        //size 为0或null
        Integer size = dto.getSize();
        if (size == 0 || size == null) {
            size=10;
        }

        //size 过大
        size=Math.min(size,10);
        dto.setSize(size);
        //tag 没传
        dto.setTag(ObjectUtils.defaultIfNull(dto.getTag(), ArticleConstants.DEFAULT_TAG));
        //minBehotTime 没传
        dto.setMinBehotTime(ObjectUtils.defaultIfNull(dto.getMinBehotTime(),new Date()));
        //maxBehotTime 没传
        dto.setMaxBehotTime(ObjectUtils.defaultIfNull(dto.getMaxBehotTime(),new Date()));
        List<ApArticle> list = apArticleMapper.loadArticleList(dto, type);
        return ResponseResult.okResult(list);
    }
}
